<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>SUPER_CTL_LEVENT_8421</title>
</head>
<style>
    html, body {
        width: 100%;
        margin: 0;
        padding: 0;
    }

    header {
        width: 100%;
        height: 40px;
        background-color: #3171FA;
        color: white;
        line-height: 40px;
        text-align: center;
    }

    footer {
        margin-top: 12px;
        width: 100%;
        background-color: #eeeeee;
    }

    footer p {
        text-align: center;
        color: #666666;
        margin: 0;
        line-height: 20px;
        font-size: 14px;
    }

    .content {
        width: 100%;
    }

    .content form {
        width: 100%;
    }

    .content form .fl {
        display: flex;
        padding: 6px;
        justify-content: center;
        align-content: center;
        flex-wrap: wrap;
    }

    form .t {
        width: 100%;
        margin: 0;
        padding: 0;
        color: #666666;
        border-bottom: 1px solid #666666;
        box-shadow: 0 3px 3px #666666;
    }

    .fl label {
        display: block;
        width: 100%;
        color: #666666;
    }

    .fl input {
        width: 95%;
        outline: none;
        border-width: 1px;
        height: 32px;
        display: block;
        border-radius: 6px;
        padding-left: 6px;
    }

    button {
        display: block;
        width: 90%;
        margin: 0 auto;
        height: 40px;
        background-color: #3171FA;
        color: white;
        border: 1px white;
        border-radius: 5px;
    }

    #mask {
        position: fixed;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        background-color: rgba(0, 0, 0, .3);
        display: flex;
        align-items: center;
    }

    #mask p {
        text-align: center;
        color: white;
        font-weight: bold;
        font-size: 2rem;
        width: 100%;
    }
</style>
<body>
<header>SUPER_CTL_CONFIG</header>
<div class="content">
    <form action="#" id="form">
        <p class="t">WIFI</p>
        <div class="fl">
            <label for="ws">WIFI</label>
            <input type="text" id="ws">
        </div>
        <div class="fl">
            <label for="wp">密码</label>
            <input type="text" id="wp">
        </div>
        <p class="t">服务器</p>
        <div class="fl">
            <label for="sip">IP</label>
            <input type="text" id="sip">
        </div>
        <div class="fl">
            <label for="spt">端口</label>
            <input type="text" id="spt">
        </div>
        <div class="fl">
            <label for="su">账户</label>
            <input type="text" id="su">
        </div>
        <div class="fl">
            <label for="sp">密码</label>
            <input type="text" id="sp">
        </div>
        <div class="fl">
            <label for="std">消息(Down)</label>
            <input type="text" id="std">
        </div>
        <div class="fl">
            <label for="stu">消息(Up)</label>
            <input type="text" id="stu">
        </div>
        <div class="fl">
            <label for="ssyc">状态同步</label>
            <input type="text" id="ssyc">
        </div>
        <button>保存</button>
    </form>
</div>
<footer>
    <p>SUPER_CTL:Powered by Levent</p>
    <p>levent8421@outlook.com</p>
</footer>
<div id="mask" style="display: none">
    <p>Loading...</p>
</div>
<script>
    function Request(op) {
        this.url = op.url;
        this.method = op.method || 'get';
        this.data = op.data;
        this.success = op.success;
        this.fail = op.fail;
        this.headers = op.headers || {};
    }

    Request.prototype = {
        send: function () {
            var _this = this;
            var xhr = new XMLHttpRequest();
            xhr.open(this.method, this.url, true);
            for (var name in this.headers) {
                if (this.headers.hasOwnProperty(name)) {
                    xhr.setRequestHeader(name, this.headers[name]);
                }
            }
            xhr.onreadystatechange = function () {
                if (xhr.readyState === XMLHttpRequest.DONE) {
                    if (xhr.status === 200) {
                        var data;
                        try {
                            data = JSON.parse(xhr.responseText);
                        } catch (e) {
                            data = xhr.responseText;
                        }
                        _this.success(data);
                    } else {
                        _this.fail(xhr.status, xhr.responseText);
                    }
                }
            };
            xhr.send(this.data);
        }
    };

    function getEl(id) {
        return document.getElementById(id);
    }


    window.onload = function () {
        var mask = getEl('mask');

        function showMask(show) {
            mask.style.display = show ? 'flex' : 'none';
        }

        var inputs = ['ws', 'wp', 'sip', 'spt', 'su', 'sp', 'stu', 'std', 'ssyc'];
        var vals = {};
        for (var i in inputs) {
            var name = inputs[i];
            vals[name] = getEl(name);
        }
        var form = getEl('form');

        function checkParam() {
            for (var i in vals) {
                var ipt = vals[i];
                if (ipt.value === '' || ipt.value.trim() === '') {
                    alert('完善表单！');
                    return false;
                }
            }
            return true;
        }

        function setVal(name, val) {
            vals[name].value = val;
        }

        function loadConf() {
            showMask(true);
            new Request({
                url: '/config.json',
                method: 'get',
                success: function (res) {
                    showMask(false);
                    setVal('ws', res.wifi.ssid);
                    setVal('wp', res.wifi.password);
                    setVal('sip', res.server.msgHost);
                    setVal('spt', res.server.msgPort);
                    setVal('su', res.server.msgUser);
                    setVal('sp', res.server.msgPassword);
                    setVal('st', res.server.topic);
                    setVal('ssyc', res.server.syncUrl);
                },
                fail: function (err) {
                    showMask(false);
                    alert(err);
                }
            }).send();
        }

        form.onsubmit = function (e) {
            e.preventDefault();
            if (!checkParam()) {
                return false;
            }
            const conf = {
                wifi: {
                    ssid: vals.ws.value,
                    password: vals.wp.value,
                },
                server: {
                    msgHost: vals.sip.value,
                    msgPort: vals.spt.value,
                    msgUser: vals.su.value,
                    msgPassword: vals.sp.value,
                    topicDown: vals.std.value,
                    topicUp: vals.stu.value,
                    syncUrl: vals.ssyc.value,
                }
            };
            showMask(true);
            new Request({
                url: '/conf',
                method: 'post',
                data: JSON.stringify(conf),
                success: function (res) {
                    showMask(false);
                    alert('配置成功，请重启设备:' + res);
                },
                fail: function (err) {
                    showMask(false);
                    console.log('f', err);
                    alert(err);
                }
            }).send();
            return false;
        };
        loadConf();
    }
</script>
</body>
</html>